package top.chmin.project.cloud.builder;

import org.springframework.stereotype.Component;

/**
 * chmin
 * 2023/12/15 15:21
 * cloud-builder
 */
@Component
public class ModelFPCBuilder implements Builder {
    @Override
    public void build(BuilderConfig config) {
        String content = """
                package top.chmin.project.cloud.#e.module#.#e.pack#.auto;
                                
                // field import auto

                import top.chmin.project.cloud.common.entity.Condition;
                import top.chmin.project.cloud.common.entity.PreCondition;

                /**
                 * chmin
                 * #dateTime#
                 * cloud-builder
                 */
                public class #e.simpleName#FPC extends PreCondition {

                    public #e.simpleName#FPC() {
                    }

                    public #e.simpleName#FPC(#e.simpleName#FPC parent) {
                        super(parent);
                    }

                    public #e.simpleName#FPC forDelete() {
                        type = "DELETE";
                        return this;
                    }
                     
                    public #e.simpleName#FPC forCount() {
                        type = "COUNT";
                        return this;
                    }

                    public #e.simpleName#FPC forDistinct() {
                        type = "DISTINCT";
                        return this;
                    }

                    public #e.simpleName#FPC limit(int limit) {
                        super.limit = limit;
                        return this;
                    }

                    public #e.simpleName#FPC sub() {
                        return new #e.simpleName#FPC(this);
                    }

                    public #e.simpleName#FPC complete(boolean and) {
                        finish(and);
                        return parent == null ? this : (#e.simpleName#FPC) parent;
                    }

                    public Condition toCondition() {
                        return toCondition(#e.simpleName#.class, "#e.tableName#");
                    }

                // field fpc auto
                }""";

        save(config, config.getEntity().getClassName() + "FPC", content, true);

        config.getEntityColumnList()
                .forEach(f -> appendJava(config, f,
                        "// field import auto",
                        "import top.chmin.project.cloud.common.entity.#f.simpleType#FPCField;",
                        "import top.chmin.project.cloud.common.entity.#f.simpleType#FPCField;",
                        config.getEntity().getClassName() + "FPC"));

        config.getEntityColumnList()
                .forEach(f -> appendJava(config, f, "// field fpc auto", "public #f.methodName# #f.fieldName#()",
                        """
                                    public #f.methodName# #f.fieldName#() {
                                        return new #f.methodName#();
                                    }

                                    public class #f.methodName# extends #f.simpleType#FPCField<#e.simpleName#FPC> {
                                        private #f.methodName#() {
                                            super(#e.simpleName#FPC.this, "#f.columnName#");
                                        }
                                \t}""", config.getEntity().getClassName() + "FPC"));
    }

}
